/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package com.cxhd.lis.model.newmodel;

import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.Model;
import com.jfinal.plugin.activerecord.Page;
import com.jfinal.plugin.activerecord.Record;
import java.util.Date;
import java.util.List;

//import static java.util.Optional.empty;

/**
 *
 * @author cxhd
 */
@SuppressWarnings("serial")
public class LisTestdescribe extends Model<LisTestdescribe>  {
     public static final LisTestdescribe lisTestdescribedao=new LisTestdescribe();
     public void deleteTesd(String testid){
         Db.use("mysql").update("delete from lis_testdescribe where TESTID=?",testid);
     }
     
     public void saveTesd(String testid,String CHINESENAME,String ENGLISHNAME,String LABDEPARTMENT,String SAMPLETYPE,String UNIT,String EMERGENCY,String PRICE,String REFCLASS ){  
//PK,TESTID,ENGLISHNAME,CHINESENAME,SAMPLETYPE,UNIT,EMERGENCY,INUREDATE,
                              // PRINTORD,PRICE,QCNEED,PY,WB
    double price=0;
   if(PRICE!=""&&PRICE!=null){
   price=  Double.valueOf(PRICE);
   }
                set("TESTID", testid)
         .set("CHINESENAME", CHINESENAME)               
                 .set("LABDEPARTMENT", LABDEPARTMENT)//所属科室
                 .set("FREQUENCYTIME", 24)//频率
                  .set("QCNEED",1)
                 .set("PRINTORD", getprintord())
                 .set("ENGLISHNAME", ENGLISHNAME)
                 .set("SAMPLETYPE", SAMPLETYPE)
                 .set("UNIT", UNIT)
                 .set("EMERGENCY", EMERGENCY)
                 .set("PRICE",price )
                 .set("INUREDATE", new Date())
                 .set("REFCLASS", REFCLASS)
                 .save();
     }
     public void updateTest(String PK,String CHINESENAME,String ENGLISHNAME,String LABDEPARTMENT,String SAMPLETYPE,String UNIT,String EMERGENCY,String PRICE,String REFCLASS ){
        double price=0;
   if(PRICE!=""&&PRICE!=null&&PRICE.length()!=0){
   price=  Double.valueOf(PRICE);
   }
         findById(PK)
                 .set("CHINESENAME", CHINESENAME)               
                 .set("LABDEPARTMENT", LABDEPARTMENT)//所属科室
                 .set("FREQUENCYTIME", 24)//频率
                  .set("QCNEED",1)
//                 .set("PRINTORD", getprintord())
                 .set("ENGLISHNAME", ENGLISHNAME)
                 .set("SAMPLETYPE", SAMPLETYPE)
                 .set("UNIT", UNIT)
                 .set("EMERGENCY", EMERGENCY)
                 .set("PRICE", price)
                 .set("REFCLASS", REFCLASS)
                 .update();
                 
     }
//      public String getPk(){
//           List<Record> list=Db.use("mysql").find("select * from lis_testdescribe limit 0,10");
//        String  value = "1";
//        if(list.size()>0){
//        value = (Integer.parseInt(Db.use("mysql").findFirst("SELECT MAX(PK) as id from lis_testdescribe ").get("id") + "") + 1) + "";
//        }
//      return value;
//    }
     
     public String getprintord(){//打印顺序
           List<Record> list=Db.use("mysql").find("select * from lis_testdescribe limit 0,10");
        String  value = "1";
        if(list.size()>0){
           value = (Integer.parseInt(Db.use("mysql").findFirst("SELECT MAX(PRINTORD) as id from lis_testdescribe ").get("id") + "") + 1) + "";
        }
      return value;
      }
     
     public List<Record> getLisTests( String PK){
         return Db.use("mysql").find("select t.PK TESTPK,t.PRICE, t.ENGLISHNAME,t.CHINESENAME,t.TESTID ,t.SAMPLETYPE,\n" +
"(SELECT s.SAMPLEDESCRIBE  FROM lis_sampletype s WHERE s.SAMPLETYPE=t.SAMPLETYPE) SAMPLEDESCRIBE \n" +
 " from lis_testdescribe t WHERE  t.PK=? ",PK);
     }
     public List<Record> getLisTestByPy( String TESTID){
         return Db.use("mysql").find("select t.PK ,t.PRICE, t.ENGLISHNAME,t.CHINESENAME,t.TESTID ,t.SAMPLETYPE,\n" +
"(SELECT s.SAMPLEDESCRIBE  FROM lis_sampletype s WHERE s.SAMPLETYPE=t.SAMPLETYPE) SAMPLEDESCRIBE \n" +
 " from lis_testdescribe t WHERE  t.TESTID like '%"+TESTID+"%' ");

     }
     
     public void updatePrintOrd(){
         List<Record> list=Db.use("mysql").find("select * from lis_testdescribe ");
         for(Record tmp : list){
          findById(tmp.get("PK"))
                 .set("PRINTORD",tmp.get("PK") )
                 .update();
          }
     }
}
